//
//  HWGameLiveCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/1/5.
//

import UIKit

class HWGameLiveCell: UICollectionViewCell {
    /// 封面
    var imageView:UIImageView!
    /// 直播中背景
    var liveTagView:UIImageView!
    /// 直播中·
    var liveTagDotView:UIView!
    /// 直播中
    var liveTagLabel:UILabel!
    /// 遮罩
    var alphaView:UIView!
    /// 名字图标
    var peopleImageView:UIImageView!
    /// 图标
    var peopleNameLabel:UILabel!
    /// 热度
    var hotImageView:UIImageView!
    /// 热度
    var hotNumLabel:UILabel!
    /// 名称
    var titleLabel:UILabel!
    
    var model:HWGameLiveListModel? {
        didSet {
            imageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
//            headImageView.sd_setImage(with: URL(string: model?.face ?? ""), completed: nil)
            peopleNameLabel.text = model?.supadmin_name
            titleLabel.text = model?.name
            hotNumLabel.text = model?.hot
            
            if model?.status == "0" {
                
//                liveTagLabel.layer.removeFromSuperlayer()
//                UIColor.gradientColor(view: liveTagView, colors: [UIColor.colorWithHexString(hexString: "#5782FF").cgColor,
//                                                                  UIColor.colorWithHexString(hexString: "#5782FF").cgColor],
//                                      start: CGPoint(x: 1, y: 0.27), end: CGPoint(x: 0.61, y: 0.61))
                liveTagView.image = UIImage(named: "notOnLive")
                liveTagLabel.text = "未开播"
            }else {
                
//                UIColor.gradientColor(view: liveTagView, colors: [UIColor.colorWithHexString(hexString: "#FF4000").cgColor,
//                                                                  UIColor.colorWithHexString(hexString: "#FF805B").cgColor,
//                                                                  UIColor.colorWithHexString(hexString: "#FF5C04").cgColor,
//                                                                  UIColor.colorWithHexString(hexString: "#FF7400").cgColor], start: CGPoint(x: 1, y: 0.27), end: CGPoint(x: 0.61, y: 0.61))
                liveTagView.image = UIImage(named: "onLive")
                liveTagLabel.text = "直播中"
            }
        }
    }
    
//    var followModel:HWGameLiveListModel {
//
//    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameLiveCell {
    private func initUI() -> Void {
        imageView = UIImageView()
        imageView.backgroundColor = .clear
        imageView.layer.cornerRadius = 8*widthScale
        imageView.layer.masksToBounds = true
        imageView.contentMode = .scaleAspectFill
        contentView.addSubview(imageView)
        
        liveTagView = UIImageView(frame: CGRect(x: 0, y: 0, width: 46*widthScale, height: 16*widthScale))
        imageView.addSubview(liveTagView)
//        UIColor.gradientColor(view: liveTagView, colors: [UIColor.colorWithHexString(hexString: "#FF4000").cgColor,
//                                                          UIColor.colorWithHexString(hexString: "#FF805B").cgColor,
//                                                          UIColor.colorWithHexString(hexString: "#FF5C04").cgColor,
//                                                          UIColor.colorWithHexString(hexString: "#FF7400").cgColor], start: CGPoint(x: 1, y: 0.27), end: CGPoint(x: 0.61, y: 0.61))
        liveTagView.addCorner(conrners: [.topLeft,.bottomRight], radius: 6*widthScale)
        
        
        liveTagDotView = UIView()
        liveTagDotView.backgroundColor = .white
        liveTagDotView.layer.cornerRadius = 2*widthScale
        liveTagView.addSubview(liveTagDotView)
        
        liveTagLabel = HWLabel.createLabel(title: "直播中", color: "#FFFFFF", font: 10*widthScale)
        liveTagView.addSubview(liveTagLabel)
        
        alphaView = UIView(frame: CGRect(x: 0, y: 0, width: 172*widthScale, height: 22*widthScale))
        imageView.addSubview(alphaView)
        UIColor.gradientColor(view: alphaView, colors: [UIColor.colorWithHexString(hexString: "#000000").cgColor,UIColor.colorWithAlphaHexString(hexString: "#000000", alpha: 0).cgColor], start: CGPoint(x: 0, y: 1), end: CGPoint(x: 0, y: 0))
        
        peopleImageView = UIImageView()
        peopleImageView.image = UIImage(named: "gameLivePeople")
        imageView.addSubview(peopleImageView)
        
        peopleNameLabel = HWLabel.createLabel(title: "-", color: "#FFFFFF", font: 10*widthScale)
        peopleNameLabel.numberOfLines = 1
        imageView.addSubview(peopleNameLabel)
        
        hotImageView = UIImageView()
        hotImageView.image = UIImage(named: "gameLiveHot")
        imageView.addSubview(hotImageView)
        
        hotNumLabel = HWLabel.createLabel(title: "0", color: "#FFFFFF", font: 10*widthScale)
        imageView.addSubview(hotNumLabel)
        
        titleLabel = HWLabel.createBoldLabel(title: "-", color: "#333333", font: 12*widthScale)
        contentView.addSubview(titleLabel)
    }
    
    private func initFrame() -> Void {
        imageView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(96*widthScale)
        }
        
        liveTagView.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.height.equalTo(16*widthScale)
            make.width.equalTo(46*widthScale)
        }
        
        liveTagDotView.snp.makeConstraints { make in
            make.left.equalTo(5*widthScale)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(4*widthScale)
        }
        
        liveTagLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(liveTagDotView.snp.right).offset(2*widthScale)
        }
        
        alphaView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(22*widthScale)
        }
        
        peopleImageView.snp.makeConstraints { make in
            make.left.equalTo(6*widthScale)
            make.bottom.equalTo(-6*widthScale)
            make.width.equalTo(12*widthScale)
            make.height.equalTo(10*widthScale)
        }
        
        peopleNameLabel.snp.makeConstraints { make in
            make.left.equalTo(peopleImageView.snp.right).offset(4*widthScale)
            make.centerY.equalTo(peopleImageView)
            make.right.lessThanOrEqualTo(hotImageView.snp.left).offset(-20*widthScale)
        }
        
        hotNumLabel.snp.makeConstraints { make in
            make.right.equalTo(-8*widthScale)
            make.centerY.equalTo(peopleImageView)
        }
        
        hotImageView.snp.makeConstraints { make in
            make.width.equalTo(7*widthScale)
            make.height.equalTo(9*widthScale)
            make.centerY.equalTo(peopleImageView)
            make.right.equalTo(hotNumLabel.snp.left).offset(-2*widthScale)
        }
        
        hotNumLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(6*widthScale)
            make.right.equalTo(-6*widthScale)
            make.top.equalTo(imageView.snp.bottom).offset(8*widthScale)
            make.height.equalTo(17*widthScale)
        }
    }
}
